<%@ page import="db.ExpenseDAO" %>
<%@ page import="beans.ExpenseBean" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>公司用车管理系统（财务）</title>
    <style>
        /* 保持与expenseClaim.jsp相同的样式 */
        body {
            margin: 0;
            padding: 0;
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: #f6f6f6;
        }
        .header {
            background: #183a8a;
            color: #fff;
            font-size: 2rem;
            font-weight: bold;
            padding: 10px 0 10px 10px;
            letter-spacing: 2px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .header-right {
            display: flex;
            align-items: center;
            margin-right: 30px;
            font-size: 1rem;
        }
        .user-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #fff;
            color: #183a8a;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2rem;
            margin-right: 10px;
        }
        .user-info {
            margin-right: 20px;
            color: #fff;
        }
        .bell-icon {
            font-size: 1.3rem;
            margin-right: 20px;
            color: #fff;
        }
        .logout {
            color: #fff;
            text-decoration: none;
            margin-left: 10px;
            font-size: 1rem;
        }
        .container {
            display: flex;
            height: calc(100vh - 56px);
        }
        .sidebar {
            width: 220px;
            background: #183a8a;
            display: flex;
            flex-direction: column;
            padding-top: 10px;
            box-shadow: 2px 0 8px rgba(24,58,138,0.05);
        }
        .menu-item {
            color: #fff;
            padding: 18px 0 18px 32px;
            font-size: 1.15rem;
            cursor: pointer;
            border-left: 5px solid transparent;
            transition: background 0.2s, border 0.2s;
            text-decoration: none;
            display: block;
        }
        .menu-item.active, .menu-item:hover {
            background: #2450a7;
            border-left: 5px solid #fff;
            font-weight: bold;
            box-shadow: 0 4px 8px rgba(24,58,138,0.10);
        }
        .main-content {
            flex: 1;
            background: #f6f6f6;
            border: 2px solid #3ba0e9;
            border-left: none;
            padding: 20px;
        }
        .expenseClaim {
            background: #fff;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .claim-list {
            max-height: 400px;
            overflow-y: auto;
            margin-top: 20px;
        }
        .claim-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px solid #ddd;
        }
        .claim-item:last-child {
            border-bottom: none;
        }
        .claim-item .status {
            background: #f0f0f0;
            padding: 5px 10px;
            border-radius: 4px;
            color: #333;
        }
        .claim-item .status.approved {
            background: #d4edda;
            color: #0f5132;
        }
        .claim-item .status.pending {
            background: #fff3cd;
            color: #664d03;
        }
        .claim-item .status.rejected {
            background: #f8d7da;
            color: #721c24;
        }
        .search-section {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            margin-bottom: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 5px;
        }
        .search-field {
            display: flex;
            flex-direction: column;
            min-width: 180px;
        }
        .search-field label {
            margin-bottom: 5px;
            font-weight: bold;
            color: #495057;
        }
        .search-field input, .search-field select {
            padding: 8px 12px;
            border: 1px solid #ced4da;
            border-radius: 4px;
            font-size: 14px;
        }
        .search-buttons {
            display: flex;
            align-items: flex-end;
            gap: 10px;
        }
        .search-btn {
            padding: 8px 20px;
            background-color: #183a8a;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .search-btn:hover {
            background-color: #2450a7;
        }
        .reset-btn {
            padding: 8px 20px;
            background-color: #6c757d;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .reset-btn:hover {
            background-color: #5a6268;
        }
        .no-records {
            text-align: center;
            padding: 20px;
            color: #6c757d;
            font-size: 1.1rem;
        }
        .error-message {
            color: #dc3545;
            padding: 15px;
            background-color: #f8d7da;
            border-radius: 4px;
            margin-top: 20px;
        }
        .claim-item > div {
            flex: 1;
            min-width: 120px;
            padding: 0 5px;
        }
        .claim-item .status {
            min-width: 80px;
            text-align: center;
        }
        .claim-item .action-btn {
            padding: 5px 10px;
            background-color: #183a8a;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
            display: inline-block;
            margin-left: 10px;
        }
        .claim-item .action-btn:hover {
            background-color: #2450a7;
        }
        .claim-item .actions {
            min-width: 100px;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="header">
    <div>欢迎登录公司用车管理系统（财务）</div>
    <div class="header-right">
        <div class="user-icon">👤</div>
        <div class="user-info">
            员工: <%= session.getAttribute("employeeName") %>
            (<%= session.getAttribute("position") %>)
        </div>
        <a href="LogoutServlet" class="logout">退出登录</a>
    </div>
</div>
<div class="container">
    <div class="sidebar">
        <a class="menu-item" href="firstpage.jsp">系统首页</a>
        <a class="menu-item" href="Profile.jsp">个人中心</a>
        <a class="menu-item" href="myTo-Do.jsp">我的待办</a>
        <a class="menu-item active" href="Cost.jsp">用车费用报销单</a>
        <a class="menu-item" href="expenseClaim.jsp">维修费用报销单</a>
    </div>
    <div class="main-content">
        <div class="expenseClaim">
            <!-- 搜索区域 -->
            <form method="get" action="Cost.jsp">
                <div class="search-section">
                    <div class="search-buttons">
                        <button type="submit" class="search-btn">搜索</button>
                        <a href="Cost.jsp" class="reset-btn">重置</a>
                    </div>

                    <div class="search-field">
                        <label for="cardNumber">卡号</label>
                        <input type="text" id="cardNumber" name="cardNumber"
                               value="<%= request.getParameter("cardNumber") != null ? request.getParameter("cardNumber") : "" %>"
                               placeholder="请输入卡号">
                    </div>

                    <div class="search-field">
                        <label for="type">类型</label>
                        <select id="type" name="type">
                            <option value="">全部类型</option>
                            <option value="油费" <%= "油费".equals(request.getParameter("type")) ? "selected" : "" %>>油费</option>
                            <option value="过路费" <%= "过路费".equals(request.getParameter("type")) ? "selected" : "" %>>过路费</option>
                            <option value="停车费" <%= "停车费".equals(request.getParameter("type")) ? "selected" : "" %>>停车费</option>
                            <option value="其他" <%= "其他".equals(request.getParameter("type")) ? "selected" : "" %>>其他</option>
                        </select>
                    </div>

                </div>
            </form>

            <div class="claim-list">
                <%
                    // 获取搜索参数
                    String cardNumber = request.getParameter("cardNumber");
                    String type = request.getParameter("type");
                    String startDate = request.getParameter("startDate");
                    String endDate = request.getParameter("endDate");

                    ExpenseDAO dao = new ExpenseDAO();
                    try {
                        List<ExpenseBean> records= new ArrayList<>();

                        // 如果有搜索条件，调用搜索方法
                        if (cardNumber != null && !cardNumber.isEmpty() ||
                                type != null && !type.isEmpty() ||
                                startDate != null && !startDate.isEmpty() ||
                                endDate != null && !endDate.isEmpty()) {

                            records = dao.searchExpenseRecords(cardNumber, type, startDate, endDate);
                        } else {
                            // 没有搜索条件，获取所有记录
                            records = dao.getExpenseRecords();
                        }

                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                        if (records != null && !records.isEmpty()) {
                            for (ExpenseBean record : records) {
                                String statusClass = record.getApprovedBy() > 0 ? "approved" : "pending";
                                String statusText = record.getApprovedBy() > 0 ? "已审批" : "待审批";
                %>
                <div class="claim-item">
                    <div>单号: <%= record.getExpenseId() %></div>
                    <div>卡号: <%= record.getCardNumber() != null ? record.getCardNumber() : "N/A" %></div>
                    <div>类型: <%= record.getType() != null ? record.getType() : "N/A" %></div>
                    <div>描述: <%= record.getDescription() != null ? record.getDescription() : "N/A" %></div>
                    <div>金额: <%= record.getAmount() != null ? "¥" + record.getAmount() : "N/A" %></div>
                    <div>日期: <%= record.getDate() != null ? dateFormat.format(record.getDate()) : "N/A" %></div>
                    <div class="status <%= statusClass %>"><%= statusText %></div>
                    <div class="actions">
                        <a href="viewExpense.jsp?id=<%= record.getExpenseId() %>" class="action-btn">查看</a>
                        <% if (record.getApprovedBy() == 0) { %>
                        <a href="ApproveExpenseServlet?id=<%= record.getExpenseId() %>&action=approve" class="action-btn">审批</a>
                        <% } %>
                    </div>
                </div>
                <%
                    }
                } else {
                %>
                <div class="no-records">没有找到符合条件的记录</div>
                <%
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                %>
                <div class="error-message">查询数据库时出错: <%= e.getMessage() %></div>
                <%
                    }
                %>
            </div>
        </div>
    </div>
</div>
</body>
</html>